﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using EntityFramework.Extensions;
using DevExpress.Web.Mvc;
namespace Webcar.Controllers
{
    public partial class CarSendController : BaseControler
    {
        [HttpGet]
        public ActionResult List()
        {
            return View();
        }
        public ActionResult ListPartialView(string id)
        {
            return PartialView();
        }
        public ActionResult Delete(string id)
        {
            CurrentUser.EnsurePermission(PermissionManager.填写派车单);
            using (var DB = new WebcarContext())
            {
                var model = DB.BS_CAR_SEND.Find(id);
                if (model != null)
                {
                    DB.BS_CAR_SEND.Remove(model);
                    DB.SaveChanges();
                    return Success("List", "CarSend", new { id = model.DEPT_CODE });
                }
                else
                {
                    return Success("List", "CarSend");
                }
                
            }
            
        }
        [HttpGet]
        public ActionResult Edit(string id)
        {
            BS_CAR_SEND model = null;
            if (id.IsNotNull())
            {
                model = WebcarDataProvider.DB.BS_CAR_SEND.Find(id);
            }
            if (model == null)
            {
                model = new BS_CAR_SEND();
                model.START_DATE = DateTime.Now;
                model.END_DATE = DateTime.Now;
                model.is_check = "F";
                model.sqr = CurrentUser.GetLoginUserName();
            }
            else
            {
                if(model.send_man.IsNull())
                    model.send_man = CurrentUser.GetLoginUserName();
            }
            ViewBag.RecordNo = null;
            if (model.car_no.IsNotNull())
            {
                var car = WebcarDataProvider.DB.BS_CAR_RECORD.Find(model.car_no);
                if (car != null)
                    ViewBag.RecordNo = car.RECORD_NO;
            }
            return View(model: model);
        }
        [HttpPost]
        public ActionResult Edit(BS_CAR_SEND model)
        {
            
            CurrentUser.EnsurePermission(PermissionManager.填写派车单);
            if (model.is_check=="F"&&model.car_no.IsNotNull()&&CurrentUser.CheckPermission(PermissionManager.派车审核权限))
            {
                model.is_check = "T";
            }
            string error = "";
            if (CheckCarModel(model, ref error) == false)
            {
                ViewBag.ErrorMessage = error;
                ViewBag.RecordNo = null;
                if (model.car_no.IsNotNull())
                {
                    var car = WebcarDataProvider.DB.BS_CAR_RECORD.Find(model.car_no);
                    if (car != null)
                        ViewBag.RecordNo = car.RECORD_NO;
                }
                return View(model: model);
            }
            BS_CAR_SEND item = null;
            using (var DB = new WebcarContext())
            {
                
                if (model.XH.IsNotNull())
                {
                    item = DB.BS_CAR_SEND.Find(model.XH);
                }
                else
                {
                    item = new BS_CAR_SEND();
                    DB.BS_CAR_SEND.Add(item);
                }

                model.CopyPropertyTo(item);
                if (item.XH.IsNull())
                    item.XH = "M" + DB.GetSearialId();
                if (item.sqdate == null)
                    item.sqdate = DateTime.Now;
                if (item.is_check == "T")
                {
                    if (item.check_date == null)
                    {
                        item.check_date = DateTime.Now;
                    }
                }
                DB.SaveChanges();
            }
            return Success("List", "CarSend", new { id = item.DEPT_CODE });
        }

        bool CheckCarModel(BS_CAR_SEND model, ref string error)
        {
            if (model.DEPT_CODE.IsNull())
            {
                error = "请选择单位。";
                return false;
            }
            if (model.dept2.IsNull())
            {
                error = "请选择用车部门。";
                return false;
            }
            if (model.START_DATE==null)
            {
                error = "请选择出车时间。";
                return false;
            }
            if (model.sqr.IsNull())
            {
                error = "请输入申请人。";
                return false;
            }
            if (model.use_nums==null||model.use_nums<=0)
            {
                error = "请输入用车人数。";
                return false;
            }
            if (model.is_check != "T") return true;
            if (model.car_no.IsNull())
            {
                error = "请输入车牌号。";
                return false;
            }
            if (model.driver_name.IsNull())
            {
                error = "请输入驾驶员。";
                return false;
            }
            if (model.send_man.IsNull())
            {
                error = "请输入派车人。";
                return false;
            }
            return true;
        }
        public ActionResult Print(string id)
        {
            BS_CAR_SEND model = WebcarDataProvider.DB.BS_CAR_SEND.Find(id);
            if (model == null) return View();
            var dept = WebcarDataProvider.DB.sys_dept.Find(model.DEPT_CODE);
            if (dept != null)
            {
                ViewBag.DeptName = dept.deptname;
            }
            var childdept = WebcarDataProvider.DB.sys_dept.Find(model.dept2);
            if (childdept != null)
            {
                ViewBag.ChildDeptName = childdept.deptname;
            }

            ViewBag.RecordNo = "无";
            var car = WebcarDataProvider.DB.BS_CAR_RECORD.Find(model.car_no);
            if (car != null)
            {
                ViewBag.RecordNo = car.RECORD_NO;
            }
            if (model.sqdate == null)
            {
                model.sqdate = model.START_DATE;
            }
            return View(model:model);
        }
    }
}